<?php
//@author nankuifeng add 2013/1/5
	class SpecialPublicInfo extends AppModel {
		var $name="SpecialPublicInfo";
		var $useTable="special_public_info";
		var $primaryKey = 'id';

		/**
		 * 特別公開企業の取得
		 * @param unknown_type $member_id
		 */
		function getSpecialCompanyByMemberID($member_id) {
			$sql = "
					select distinct
							t.company_id,	
							c.company_name,
							sp.public_flg
					from  notice_msg_info as s 			
						join notice_msg_reaction_info as sr on sr.notice_id = s.id	and sr.deleted is null	
						join notice_thema_info as t on t.thema_id = s.thema_id and t.deleted is null		
						join company_info as c on c.id = t.company_id	
						left join special_public_info sp 
						 on sp.company_id = t.company_id and sp.member_id = sr.member_id 
					where s.deleted is null and s.notice_status = '0' and sp.deleted is null 			
					 and sr.member_id = " . $member_id;
			 $data = $this->query($sql);
			 $special = Array();
			 for($i = 0 ; $i < count($data); $i++) {
			 	$special['special'][$data[$i][0][company_id]]['public_flg'] = $data[$i][0][public_flg];
			 	$special['special'][$data[$i][0][company_id]]['company_name'] = $data[$i][0][company_name];
			 }
			 
			 return $special;
		}
		
		/**
		 * 特別公開企業を削除する
		 * @param int $company_id
		 * @param int $member_id
		 */		
		function doDeleteSpecialCompany($company_id, $member_id) {
			$sql = "select id from special_public_info " 
			      . " where company_id = " .$company_id 
			      . " and member_id = ". $member_id;			
			$dat = $this->query($sql);
			
			if(!empty($dat) && count($dat) > 0 ) {
				$sql = "update special_public_info set deleted = now() - interval '1 hour',public_flg = '0' "
				. " where company_id = " .$company_id
				. " and member_id = ". $member_id;
				$this->query($sql);
			} else {
				$sql = "insert into special_public_info (member_id,  company_id,  public_flg,  created,  deleted,  modified	)	
				select " 
				    . $member_id 
				    . "," .$company_id 
				    . ",'0'
				      , now() - interval '1 hour'
				      , now() - interval '1 hour'
				      , now() - interval '1 hour'
				      ";
				$this->query($sql);				
			}
		}
		
		/**
		 * 特別公開企業を保存する
		 * @param Array $special
		 * @param int $member_id
		 */
		function doSaveSpecialCompany($public_flg, $special, $member_id) {
			if($public_flg == '3') {
				//スカウト利用しない場合、特別公開データをクリアする
				$sql = "update special_public_info set modified = now() - interval '1 hour', public_flg = '0' "
				. " where member_id = ". $member_id;
				$this->query($sql);
				return;
			} else if($public_flg == '2') {
				//すべて会社に公開の場合、特別公開を更新しない
				return;
			}
			
			
			if($special == null || empty($special) || count($special) < 1 ) {
				return;
			}
			foreach($special as $key => $sp) {
				$company_id = $key;
				$flg = $sp['public_flg'];
				
				$sql = "select id from special_public_info "
				. " where company_id = " .$company_id
				. " and member_id = ". $member_id;
				$dat = $this->query($sql);
			
				if(!empty($dat) && count($dat) > 0 ) {
					$sql = "update special_public_info set modified = now() - interval '1 hour'
					   ,public_flg = " . $flg . " , deleted = null "
					. " where company_id = " .$company_id
					. " and member_id = ". $member_id;
					$this->query($sql);
				} else {
					$sql = "insert into special_public_info (member_id,  company_id,  public_flg,  created,  deleted,  modified	)
					select "
					. $member_id
					. "," .$company_id
					. "," .$flg ."
					, now() - interval '1 hour'
					, null
					, now() - interval '1 hour'
					";
					$this->query($sql);
				}
			}
		}		
		
	}
?>